package com.merlin.demo_wellshape.exception;

import com.merlin.demo_wellshape.pojo.ResponseMessage;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice // 拦截所有Controller抛出的异常
public class GlobalExceptionHandlerAdvice {

    Logger logger = LoggerFactory.getLogger(GlobalExceptionHandlerAdvice.class);

    @ExceptionHandler({Exception.class}) // 拦截所有Exception异常
    public ResponseMessage<String> handleException(Exception e, HttpServletRequest request, HttpServletResponse response) {
        // 记录日志
        logger.error("统一异常:", e);
        return new ResponseMessage(500, e.getMessage(), null);
    }
}
